package com.sinodata.bsm.center.service.event;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.EventRuleBo;
import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.cache.DefaultEventExcludeRuleCache;
import com.sinodata.bsm.center.cache.DefaultEventRuleCache;
import com.sinodata.bsm.center.cache.EventRuleCache;
import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.cache.ResTypeCache;
import com.sinodata.bsm.center.dao.impl.EventRuleDaoImpl;
import com.sinodata.bsm.common.vo.DefaultEventExcludeRule;
import com.sinodata.bsm.common.vo.DefaultEventRule;
import com.sinodata.bsm.common.vo.EventRule;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-19 PM 3:05:15     liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class EventRuleService {

    @Autowired
    private EventRuleCache eventRuleCache;
    @Autowired
    private ResCache resCache;
    @Autowired
    private ResTypeCache resTypeCache;
    @Autowired
    private DefaultEventRuleCache defaultEventRuleCache;
    @Autowired
    private DefaultEventExcludeRuleCache defaultEventExcludeRuleCache;
    @Autowired
    private EventRuleDaoImpl eventRuleDao;

    public Page<DefaultEventRule> findDefaultEventRuleByPage(Page<DefaultEventRule> page, Condition condition) {
        return eventRuleDao.findDefaultEventRuleByPage(page, condition);
    }

    public List<EventRuleBo> findByResId(Long resId) {
        return eventRuleDao.findByRes(resId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void add(EventRule eventRule) {
        eventRuleDao.save(eventRule);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void addDefaultEventExcludeRule(DefaultEventExcludeRule defaultEventExcludeRule) {
        eventRuleDao.save(defaultEventExcludeRule);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void update(EventRule eventRule) {
        eventRuleDao.update(eventRule);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteDefaultEventExcludeRule(Long id) {
        eventRuleDao.delete(defaultEventExcludeRuleCache.get(id));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(Long id) {
        eventRuleDao.delete(get(id));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteByResId(Long resId) {
        eventRuleDao.deleteByResId(resId);
    }

    public EventRule get(Long id) {
        return eventRuleCache.get(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void batchApplySave(List<EventRule> list) throws Exception {
        eventRuleDao.batchSave(list);
    }

    /**
     * 获取资源的默认事件规则，包含继承的父类别的事件规则,只包含全局启用的
     * 
     * @param resId
     * @return
     */
    public List<DefaultEventRule> getDefaultEventRuleByResId(Long resId) {
        Long resTypeId = resCache.get(resId).getResTypeId();
        List<DefaultEventRule> defaultEventRuleList = new ArrayList<DefaultEventRule>();
        this.getDefaultEventRuleList(defaultEventRuleList, resTypeId);
        List<DefaultEventRule> list = new ArrayList<DefaultEventRule>();
        if (defaultEventRuleList != null) {
            for (DefaultEventRule defaultEventRule : defaultEventRuleList) {
                //全局启用的
                if (defaultEventRule.getStatus().equals(1)) {
                    list.add(defaultEventRule);
                }
            }
        }
        return list;
    }

    //TODO liu 改
    private List<DefaultEventRule> getDefaultEventRuleList(List<DefaultEventRule> result, Long resTypeId) {
        Long parentId = resTypeCache.get(resTypeId).getParentId();
        //  
        if (parentId != null) {
            getDefaultEventRuleList(result, parentId);
        }
        List<DefaultEventRule> temp = defaultEventRuleCache.getDefaultEventRuleByResTypeId(resTypeId);
        if (temp != null && temp.size() > 0) {
            result.addAll(temp);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDefaultEventRule(DefaultEventRule defaultEventRule) {
        eventRuleDao.update(defaultEventRule);
    }
}